package main

import (
	"fmt"
	"zinx/MMO_Game_Zinx/apis"
	"zinx/MMO_Game_Zinx/core"
	"zinx/Zinx/ziface"
	"zinx/Zinx/znet"
)

func onConnectionStart(conn ziface.IConnection) {
	// 初始化Player
	player := core.NewPlayer(conn)
	// 将pid绑定给conn
	conn.SetProperty("pid", player.Pid)
	// 玩家上线
	player.Online()
	fmt.Printf("[Player]Pid= %d -->Enter the game\n", player.Pid)
}
func onConnectionStop(conn ziface.IConnection) {
	// 获得player
	pid, _ := conn.GetProperty("pid")
	player := core.WMObj.GetPlayerByPid(pid.(int32))
	// 发送消息给其他客户端
	fmt.Printf("[Player]Pid= %d -->Leave the game\n", player.Pid)
	player.BroadCastTalk(fmt.Sprintf("[Player]Pid= %d -->Leave the game\n", player.Pid))
	// 玩家下线
	player.Offline()
}

func main() {
	// 创建服务
	s := znet.NewServer()
	// 注册钩子
	s.SetOnConnStart(onConnectionStart)
	s.SetOnConnStop(onConnectionStop)
	// s.SetOnConnStop()
	// 注册路由
	// 处理msgID=2, 世界聊天消息
	s.AddRouter(2, &apis.WorldChatApi{})
	// 处理msgID=3, 人物坐标更新
	s.AddRouter(3, &apis.MoveApi{})
	// 启动服务
	s.Server()
}
